package com.x.game.model;


import java.util.Objects;

/**
 * 值对象：棋盘尺寸
 * 不可变，且只校验行列>=2
 */
public class BoardSize {


    private final int rows;
    private final int cols;

    /* ---------- 预定义常用尺寸 ---------- */
    public static final BoardSize SIZE_4X4 = new BoardSize(4, 4);
    public static final BoardSize SIZE_8X8 = new BoardSize(8, 8);

    public BoardSize(int rows, int cols) {
        if (rows < 2 || cols < 2) {
            throw new IllegalArgumentException("行列必须>=2");
        }
        this.rows = rows;
        this.cols = cols;
    }

    public int rows() { return rows; }
    public int cols() { return cols; }
    public int slots() { return rows * cols; }

    /* ---------- 值对象相等性 ---------- */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BoardSize)) return false;
        BoardSize boardSize = (BoardSize) o;
        return rows == boardSize.rows && cols == boardSize.cols;
    }

    @Override
    public int hashCode() {
        return Objects.hash(rows, cols);
    }

    @Override
    public String toString() {
        return rows + "x" + cols;
    }

    /* ---------- 工厂：从字符串str 例如4x4 反解析 ---------- */
    public static BoardSize of(String str) {
        String[] a = str.split("x");
        return new BoardSize(Integer.parseInt(a[0]), Integer.parseInt(a[1]));
    }
}
